﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class OrderDataAccess
    {
        #region 单例模式
        private OrderDataAccess()
        {
        }

        private static OrderDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static OrderDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new OrderDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        #region 充值订单
        /// <summary>
        /// 创建充值订单
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="amt"></param>
        /// <param name="chargeType"></param>
        /// <returns></returns>
        public long CreateChargeOrder(string orderId, string billNo, long uid, int business_order_type, long orderMasterId, decimal amt, int channelType, string remark, out string stroutput)
        {
            stroutput = "创建充值订单失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_chargeorder_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, orderId);
                db.AddInParameter(Procdbcomm, "@order_amt", DbType.Decimal, amt);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@business_order_type", DbType.Int32, business_order_type);
                db.AddInParameter(Procdbcomm, "@order_master_id", DbType.Int64, orderMasterId);
                db.AddInParameter(Procdbcomm, "@billno", DbType.String, billNo);
                db.AddInParameter(Procdbcomm, "@channel_type", DbType.Int16, channelType);
                db.AddInParameter(Procdbcomm, "@remark", DbType.String, remark);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("创建充值订单-CreateChargeOrder" + ex.ToString());
                return -99;
            }
        }

        /// <summary>
        /// 充值订单更新-支付成功
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="sellOrderNo"></param>
        /// <param name="succAmt"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public int ChargeOrderSucc(string orderId, string sellOrderNo, decimal succAmt, out long order_master_id, out int business_order_type, out string stroutput)
        {
            int ret = 0;
            order_master_id = 0;
            business_order_type = 0;
            stroutput = "更新失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_chargeorder_succ";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, orderId);
                db.AddInParameter(Procdbcomm, "@sell_order_no", DbType.String, sellOrderNo);
                db.AddInParameter(Procdbcomm, "@succ_amt", DbType.Decimal, succAmt);
                db.AddOutParameter(Procdbcomm, "@business_order_type", DbType.Int32, 8);
                db.AddOutParameter(Procdbcomm, "@order_master_id", DbType.Int64, 16);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                order_master_id = (long)db.GetParameterValue(Procdbcomm, "@order_master_id");
                business_order_type = (int)db.GetParameterValue(Procdbcomm, "@business_order_type");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("充值订单更新-支付成功-ChargeOrderSucc" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 充值订单更新-支付失败
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="sellErrorCode"></param>
        /// <param name="sellErrorMsg"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public int ChargeOrderFail(string orderId, string sellErrorCode, string sellErrorMsg, out string stroutput)
        {
            int ret = 0;
            stroutput = "更新失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_chargeorder_succ";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, orderId);
                db.AddInParameter(Procdbcomm, "@sell_error_code", DbType.String, sellErrorCode);
                db.AddInParameter(Procdbcomm, "@sell_error_msg", DbType.String, sellErrorMsg);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("充值订单更新-支付失败-ChargeOrderFail" + ex.ToString());
                ret = -99;
            }
            return ret;
        }
        #endregion

        #region 交易订单
        public long IdCreate(string id_code, out string stroutput)
        {
            long ret = 0;
            stroutput = "";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_id_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@id_code", DbType.String, id_code);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 16);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 20);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("生成id-IdCreate" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 获取用户推荐费用
        /// </summary>
        /// <returns></returns>
        public DataTable GetRecommendAmt(long uid, string begtime, string endtime)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = @"SELECT ISNULL(SUM(recommend_money),0) as recommendAmt FROM ADZ_OrderSettlement 
                                  WHERE recommend_uid = @uid AND create_time > @begin_time AND create_time<@end_time";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@begin_time", DbType.String, begtime);
                db.AddInParameter(Procdbcomm, "@end_time", DbType.String, endtime);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取用户推荐费用-GetRecommendAmt" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 获取用户销售/服务费用
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="begtime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        public DataTable GetSaleAmt(long uid, string begtime, string endtime)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = @"SELECT ISNULL(SUM(total_money),0) as saleAmt,ISNULL(SUM(buyer_money),0) as buyAmt FROM ADZ_OrderSettlement 
                                  WHERE uid = @uid AND create_time > @begin_time AND create_time<@end_time";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@begin_time", DbType.String, begtime);
                db.AddInParameter(Procdbcomm, "@end_time", DbType.String, endtime);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取用户销售/服务费用-GetSaleAmt" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 根据状态统计订单数
        /// </summary>
        /// <returns></returns>
        public List<OrderCount> GetOrderCountByStatus(long uid)
        {
            List<OrderCount> list = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = @"select order_status as status,count(order_status) as num from ADZ_OrderMaster where uid = @uid and is_del=0 group by order_status";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    list = new ModelHandler<OrderCount>().FillModel(ds.Tables[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据状态统计订单数-GetOrderCountByStatus" + ex.ToString());
            }
            return list;
        }

        /// <summary>
        /// 平台费用写入
        /// </summary>
        /// <returns></returns>
        public int OrderSettlementWrite(long order_id, out string stroutput)
        {
            int ret = 0;
            stroutput = "平台费用写入失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_ordersettlement_write";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_id", DbType.Int64, order_id);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("平台费用写入-OrderSettlementWrite" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 交易订单支付成功
        /// </summary>
        /// <param name="pay_type">2 余额 3 支付宝 4 微信</param>
        /// <returns></returns>
        public int OrderPaySucc(long order_id, int pay_type, out string stroutput)
        {
            int ret = 0;
            stroutput = "更新失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_ordermaster_paysucc";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_id", DbType.Int64, order_id);
                db.AddInParameter(Procdbcomm, "@pay_type", DbType.Int16, pay_type);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("交易订单支付成功-OrderPaySucc" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 创建交易订单
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="address_id"></param>
        /// <param name="invoice_id"></param>
        /// <param name="order_no"></param>
        /// <param name="pay_type"></param>
        /// <param name="total_money">订单总金额</param>
        /// <param name="order_source"></param>
        /// <param name="remark"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long CreateOrderMaster(long uid, long address_id, long invoice_id, string order_no, int pay_type,
            decimal total_money, int order_source, string remark, out string stroutput)
        {
            long ret = 0;
            stroutput = "创建交易订单失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_ordermaster_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@address_id", DbType.Int64, address_id);
                db.AddInParameter(Procdbcomm, "@invoice_id", DbType.Int64, invoice_id);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, order_no);
                db.AddInParameter(Procdbcomm, "@pay_type", DbType.Int16, pay_type);
                db.AddInParameter(Procdbcomm, "@total_money", DbType.Decimal, total_money);
                db.AddInParameter(Procdbcomm, "@order_source", DbType.Int16, order_source);
                db.AddInParameter(Procdbcomm, "@remark", DbType.String, remark);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("创建交易订单-CreateOrderMaster" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 创建预热商品订单
        /// </summary>
        /// <param name="order_id">交易订单id</param>
        /// <param name="goods_id">商品id</param>
        /// <param name="amount">购买数量 n*最小出库量</param>
        /// <param name="place_price">预定价格</param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public int CreateYureOrderDetails(long order_id, long goods_id, decimal amount, decimal place_price, out string stroutput)
        {
            int ret = 0;
            stroutput = "创建失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "manager_orderyuredetails_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_id", DbType.Int64, order_id);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddInParameter(Procdbcomm, "@amount", DbType.Decimal, amount);
                db.AddInParameter(Procdbcomm, "@place_price", DbType.Decimal, place_price);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("创建预热商品订单-CreateYureOrderDetails" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 创建商品订单
        /// </summary>
        /// <param name="order_id">交易订单id</param>
        /// <param name="goods_id">商品id</param>
        /// <param name="amount">购买数量 n*最小出库量</param>
        /// <param name="total_money">单个商品总金额</param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public int CreateOrderDetails(long order_id, long goods_id, decimal amount, decimal total_money, out string stroutput)
        {
            int ret = 0;
            stroutput = "创建失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_orderdetails_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_id", DbType.Int64, order_id);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddInParameter(Procdbcomm, "@amount", DbType.Decimal, amount);
                db.AddInParameter(Procdbcomm, "@total_money", DbType.Decimal, total_money);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("创建商品订单-CreateOrderDetails" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 创建竞价商品订单
        /// </summary>
        /// <param name="order_id">交易订单id</param>
        /// <param name="goods_id">商品id</param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public int CreateOrderDetailsJingjia(long order_id, long goods_id, out string stroutput)
        {
            int ret = 0;
            stroutput = "创建失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_orderdetails_create_jingjia";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_id", DbType.Int64, order_id);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("创建商品订单-CreateOrderDetailsJingjia" + ex.ToString());
                ret = -99;
            }
            return ret;
        }
        /// <summary>
        /// 获取交易订单
        /// </summary>
        /// <param name="order_no"></param>
        public OrderMaster GetOrderMaster(string order_no)
        {
            OrderMaster order = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select * from ADZ_OrderMaster where order_NO=@order_no and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, order_no);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    order = new ModelHandler<OrderMaster>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取交易订单-GetOrderMaster" + ex.ToString());
            }
            return order;
        }

        /// <summary>
        /// 获取交易订单详情
        /// </summary>
        /// <param name="order_no"></param>
        /// <returns></returns>
        public OrderInfo GetOrderInfo(string order_no)
        {
            OrderInfo order = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = @"SELECT * FROM View_Order WHERE order_NO=@order_no and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, order_no);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    order = new ModelHandler<OrderInfo>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取交易订单详情-GetOrderInfo" + ex.ToString());
            }
            return order;
        }

        /// <summary>
        /// 获取订单商品信息
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public List<OrderGood> GetOrderGood(long order_id)
        {
            List<OrderGood> list = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = @"SELECT A.order_id,A.amount as buy_amount,A.add_rate,A.price,A.total_money,
                                   B.goods_id,B.MPN,B.goods_unit,B.DCN,B.product_desc,B.stock_date,B.stock_amount,
                                   B.min_amount,B.DDD,B.market_price,B.is_withdraw,B.is_del,B.category_name,
                                   B.brand_name,B.label_type,B.supplier_name,B.end_time,B.show_min_level,B.include_tax
                                   FROM ADZ_OrderDetails A
                                   LEFT JOIN Views_Goods B ON A.goods_id=B.goods_id
                                   WHERE A.order_id=@order_id";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@order_id", DbType.Int64, order_id);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    list = new ModelHandler<OrderGood>().FillModel(ds.Tables[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取订单商品信息-GetOrderGood" + ex.ToString());
            }
            return list;
        }

        /// <summary>
        /// 我的订单
        /// </summary>
        /// <returns></returns>
        public DataSet GetOrderList(long uid, int order_status, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_order_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@order_status", DbType.Int32, order_status);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("我的订单-GetOrderList" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 交易订单状态修改
        /// </summary>
        /// <param name="order_no"></param>
        /// <param name="oldStatus"></param>
        /// <param name="newStatus"></param>
        /// <returns></returns>
        public int UpdateOrderStatus(string order_no, int oldStatus, int newStatus)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "update ADZ_OrderMaster set order_status=@newStatus where order_NO=@order_no and order_status=@oldStatus and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, order_no);
                db.AddInParameter(Procdbcomm, "@oldStatus", DbType.Int16, oldStatus);
                db.AddInParameter(Procdbcomm, "@newStatus", DbType.Int16, newStatus);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("交易订单状态修改-UpdateOrderStatus" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="order_no"></param>
        /// <returns></returns>
        public int DelOrder(string order_no)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "update ADZ_OrderMaster set is_del=1 where order_NO=@order_no and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, order_no);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("删除订单-DelOrder" + ex.ToString());
                ret = -99;
            }
            return ret;
        }
        #endregion

        #region 预定报价
        /// <summary>
        /// 根据状态统计预定数
        /// </summary>
        /// <returns></returns>
        public List<OrderCount> GetPlaceCountByStatus(long uid)
        {
            List<OrderCount> list = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = @"select place_status as status,count(place_status) as num from ADZ_GoodsPlace where uid=@uid and is_del=0 group by place_status";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    list = new ModelHandler<OrderCount>().FillModel(ds.Tables[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据状态统计预定数-GetPlaceCountByStatus" + ex.ToString());
            }
            return list;
        }

        /// <summary>
        /// 预定报价状态修改
        /// </summary>
        /// <param name="order_no"></param>
        /// <param name="oldStatus"></param>
        /// <param name="newStatus"></param>
        /// <returns></returns>
        public int UpdatePlaceStatus(long seq_id, int oldStatus, int newStatus)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "update ADZ_GoodsPlace set place_status=@newStatus where seq_id=@seq_id and place_status=@oldStatus and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@seq_id", DbType.Int64, seq_id);
                db.AddInParameter(Procdbcomm, "@oldStatus", DbType.Int16, oldStatus);
                db.AddInParameter(Procdbcomm, "@newStatus", DbType.Int16, newStatus);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("预定报价状态修改-UpdatePlaceStatus" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 删除预定报价
        /// </summary>
        /// <returns></returns>
        public int DelGoodsPlace(long seq_id)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "update ADZ_GoodsPlace set is_del=1 where seq_id=@seq_id and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@seq_id", DbType.Int64, seq_id);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("删除预定报价-DelGoodsPlace" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 预定商品报价
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="goods_id"></param>
        /// <param name="goods_amount"></param>
        /// <param name="place_price"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long CreateGoodsPlace(long uid, long goods_id, decimal place_price, out string stroutput)
        {
            long ret = 0;
            stroutput = "预定商品报价失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_goodsplace_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                //db.AddInParameter(Procdbcomm, "@goods_amount", DbType.Decimal, goods_amount);
                db.AddInParameter(Procdbcomm, "@place_price", DbType.Decimal, place_price);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("预定商品报价-CreateGoodsPlace" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 获取预定商品报价
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="goods_id"></param>
        /// <returns></returns>
        public DataTable GetGoodsPlace(long uid, long goods_id)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select top 1 * from ADZ_GoodsPlace where uid=@uid and goods_id=@goods_id and is_del=0 order by place_price desc";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取预定商品报价-GetGoodsPlace" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 获取预定商品报价
        /// </summary>
        /// <returns></returns>

        public GoodsPlace GetGoodsPlace(long seq_id)
        {
            GoodsPlace info = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select * from ADZ_GoodsPlace where seq_id=@seq_id and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@seq_id", DbType.Int64, seq_id);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    info = new ModelHandler<GoodsPlace>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取预定商品报价-GetGoodsPlace" + ex.ToString());
            }
            return info;
        }

        /// <summary>
        /// 我的预定
        /// </summary>
        /// <param name="place_status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageRows"></param>
        /// <returns></returns>
        public DataSet GetPlaceList(long uid, int place_status, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_goodsplace_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@place_status", DbType.Int32, place_status);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("我的预定-GetPlaceList" + ex.ToString());
            }
            return ds;
        }
        #endregion

        #region 销售费用相关
        /// <summary>
        /// 根据供应商统计订单
        /// </summary>
        /// <returns></returns>
        public DataSet GetOrderBySupplier(long supplier_id, string supplier_name, string beg_date, string end_date, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_order_bysupplier";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@supplier_id", DbType.Int64, supplier_id);
                db.AddInParameter(Procdbcomm, "@supplier_name", DbType.String, supplier_name);
                db.AddInParameter(Procdbcomm, "@beg_date", DbType.String, beg_date);
                db.AddInParameter(Procdbcomm, "@end_date", DbType.String, end_date);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("根据供应商统计订单-GetOrderBySupplier" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 按日期结算销售费用
        /// </summary>
        /// <param name="saledate">结算日期</param>
        /// <returns></returns>
        public long SaleSettlement(string saledate, out string stroutput)
        {
            long ret = 0;
            stroutput = "结算失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_salesettlement_add";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@sale_date", DbType.Date, saledate);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("按日期结算销售费用-SaleSettlement" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 自动维护已经发生退款的订单结算
        /// </summary>
        /// <param name="saledate">结算日期</param>
        /// <returns></returns>
        public long SaleSettlementRepair(out string stroutput)
        {
            long ret = 0;
            stroutput = "";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proa_autoordersettle";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 8000);
                db.ExecuteNonQuery(Procdbcomm);

                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("自动维护已经发生退款的订单结算-SaleSettlementRepair" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 销售业绩查询
        /// </summary>
        /// <returns></returns>
        public DataSet GetSaleSettlementList(long uid, string beg_saledate, string end_saledate, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_salesettlement_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@beg_saledate", DbType.String, beg_saledate);
                db.AddInParameter(Procdbcomm, "@end_saledate", DbType.String, end_saledate);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("销售业绩查询-GetSaleSettlementList" + ex.ToString());
            }
            return ds;
        }
        #endregion

        #region 提现订单
        /// <summary>
        /// 创建提现订单
        /// </summary>
        /// <returns></returns>
        public long CreateCashOrder(long uid, string order_no, decimal cash_amt, long cash_bank_id, out string stroutput)
        {
            stroutput = "提现失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_cashorder_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, order_no);
                db.AddInParameter(Procdbcomm, "@cash_amt", DbType.Decimal, cash_amt);
                db.AddInParameter(Procdbcomm, "@cash_bank_id", DbType.Int64, cash_bank_id);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("创建提现订单-CreateCashOrder" + ex.ToString());
                return -99;
            }
        }
        #endregion

        #region 退款订单
        /// <summary>
        /// 创建退款订单
        /// </summary>
        /// <returns></returns>
        public long CreateRefundOrder(string order_no, int business_order_type, long order_master_id, decimal refund_amt, string refund_goods, string reason, string apply_user, string remark, out string stroutput)
        {
            stroutput = "创建退款订单失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_refundorder_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@order_no", DbType.String, order_no);
                db.AddInParameter(Procdbcomm, "@business_order_type", DbType.Int32, business_order_type);
                db.AddInParameter(Procdbcomm, "@order_master_id", DbType.Int64, order_master_id);
                db.AddInParameter(Procdbcomm, "@refund_amt", DbType.Decimal, refund_amt);
                db.AddInParameter(Procdbcomm, "@refund_goods", DbType.String, refund_goods);
                db.AddInParameter(Procdbcomm, "@reason", DbType.String, reason);
                db.AddInParameter(Procdbcomm, "@apply_user", DbType.String, apply_user);
                db.AddInParameter(Procdbcomm, "@remark", DbType.String, remark);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("创建退款订单-CreateRefundOrder" + ex.ToString());
                return -99;
            }
        }
        #endregion
    }
}
